package com.csx.avl;

import com.csx.map.BinarySearchTreeMap;

import java.sql.SQLOutput;
import java.util.ArrayList;

/**
 * @author 陈胤训
 * @date 2019/6/14 11:17
 * Utils: Intellij Idea
 * Description: AVL 平衡二叉树
 */
public class AVLTree<K extends Comparable<K>, V> {

    private class Node{
        /* 键*/
        public K key;

        /**
         * 值
         *  */
        public V value;

        /**
         * 左右树
         **/
        public Node left, right;

        /**
         * 高度
         */
        public int  height;

        public Node( K key, V val){
            this.key = key;
            this.value = val;
            this.left = null;
            this.right = null;
            this.height = 1;
        }


        @Override
        public String toString(){
            return key.toString();
        }
    }
    /** 根*/
    private Node root;

    /** 大小*/
    private int size;

    public AVLTree(){
        root = null;
        size = 0;
    }

    /**
     * 获取节点node的高度
     * @param node 节点
     * @return 高度
     */
    private int getHeight(Node node){
        if (node == null){
            return 0;
        }
        return node.height;
    }

    /**
     * 获取节点node的平衡因子
     * @param node 节点
     * @return 因子
     */
    private int getBalanceFactor(Node node){
        if (node == null){
            return 0;
        }
        return getHeight(node.left) - getHeight(node.right);
    }

    private Node getNode(Node node, K key){
        if (node == null){
            return null;
        }
        if (key.compareTo(node.key) == 0){
            return node;
        }else if (key.compareTo(node.key) < 0){
            return getNode(node.left, key);
        }else{
            return getNode(node.right, key);
        }

    }

    /**
     * 右旋转
     * @param y 节点
     * @return 节点
     */
    private Node rightRotate(Node y){
        Node x = y.left;
        Node T3 = x.right;

        // 向有旋转过程
        x.right = y;
        y.left = T3;

        //更新height
        y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
        x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;

        return x;
    }

    /**
     * 左旋转
     * @param y 节点
     * @return 节点
     */
    private Node leftRotate(Node y){
        Node x = y.right;
        Node T2 = x.left;

        // 向左旋转过程
        x.left = y;
        y.right = T2;

        //更新height
        y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
        x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;

        return x;
    }

    public void add(K key, V value) {
        root = add(root,key,value);
    }

    /**
     * 向以node为根的二分搜索树中插入元素e, 递归算法
     * @param node 根
     * @param key 键
     * @param value 值
     * @return 新的根
     */
    private Node add(Node node, K key, V value) {
        if (node == null){
            size ++;
            return new Node(key,value);
        }
        if (key.compareTo(node.key) < 0){
            node.left = add(node.left,key,value);
        }else if (key.compareTo(node.key) > 0){
            node.right = add(node.right,key,value);
        }else{
            node.value = value;
        }
        // 更新height
        node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right));

        // 计算平衡因子
        int balanceFactor = getBalanceFactor(node);

        if (Math.abs(balanceFactor) > 1){
            System.out.println("balanceFactor : " + balanceFactor);
        }

        //平衡维护
        //LL
        if (balanceFactor > 1 && getBalanceFactor(node.left) >= 0){
            return rightRotate(node);
        }

        //RR
        if (balanceFactor < -1 && getBalanceFactor(node.right) <= 0){
            return leftRotate(node);
        }

        //LR
        if (balanceFactor > 1 && getBalanceFactor(node.left) < 0){
            node.left = leftRotate(node.left);
            return rightRotate(node);
        }

        //Rl
        if (balanceFactor < -1 && getBalanceFactor(node.right) > 0){
            node.right = rightRotate(node.right);
            return leftRotate(node);
        }

        return node;
    }


    public V remove(K key)  {
        Node node = getNode(root, key);
        if (node != null){
            root = remove(root,key);
            return node.value;
        }
        return null;
    }


    public boolean contains(K key) {
        return getNode(root,key) != null;
    }


    public V get(K key) {
        Node node = getNode(root, key);
        return node == null ? null : node.value;
    }


    public void set(K key, V value) throws IllegalAccessException {
        Node node = getNode(root, key);
        if(node == null){
            throw new IllegalAccessException(key + " 不存在");
        }
        node.value = value;
    }


    public int gitSize() {
        return size;
    }


    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 判断该二叉树是否是一颗二分搜索树
     * @return
     */
    public Boolean isBST(){
        ArrayList<K> keys = new ArrayList<>();
        inOrder(root, keys);
        for (int i = 1; i < keys.size(); i++) {
            if (keys.get(i - 1).compareTo(keys.get(i)) > 0){
                return false;
            }
        }
        return true;
    }

    /**
     * 判断该二叉树是否是一颗平衡二叉树
     * @return
     */
    private boolean isBalanced(){
        return isBalanced(root);
    }

    /**
     * 判断以node 为根的二叉树是否是一颗平衡二叉树, 递归算法
     * @param root 节点
     * @return
     */
    private boolean isBalanced(Node root) {

        if (root == null){
            return true;
        }
        int balanceFactor = getBalanceFactor(root);
        if (Math.abs(balanceFactor) > 1){
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }

    private void inOrder(Node root, ArrayList<K> keys) {
        if (root == null){
            return;
        }
        inOrder(root.left, keys);
        keys.add(root.key);
        inOrder(root.right, keys);
    }

    /**
     * 删除掉以node为根的二分搜索树中值为e的节点, 递归算法
     * @param root 根
     * @param key 键
     * @return 删除以后的值
     */
    private Node remove(Node root, K key) {
        if (root == null){
            return null;
        }

        Node retNode;
        if (key.compareTo(root.key) < 0){
            root.left = remove(root.left,key);
            retNode = root;
        }else if (key.compareTo(root.key) > 0){
            root.right = remove(root.right,key);
            retNode = root;
        }else{
            if (root.right == null){
                Node leftNode = root.left;
                root.left = null;
                size --;
                retNode = leftNode;
            }
            else if (root.left == null){
                Node rightNode = root.right;
                root.right = null;
                size --;
                retNode = rightNode;
            }else {
                /*
                 *待删除节点左右子树均不为空的情况
                 * 找到比待删除节点大的最小节点,  即待删除节点右子树的最小节点
                 * 用这个节点顶替待删除节点的位置
                 * */
                Node minimum = minimum(root.right);
                minimum.right = remove(root.right, minimum.key);
                minimum.left = root.left;
                root.left = root.right = null;
                retNode = minimum;
            }
        }
        if (retNode == null){
            return null;
        }

        // 更新height
        retNode.height = 1 + Math.max(getHeight(retNode.left), getHeight(retNode.right));

        // 计算平衡因子
        int balanceFactor = getBalanceFactor(retNode);

        if (Math.abs(balanceFactor) > 1){
            System.out.println("balanceFactor : " + balanceFactor);
        }

        //平衡维护
        //LL
        if (balanceFactor > 1 && getBalanceFactor(retNode.left) >= 0){
            return rightRotate(retNode);
        }

        //RR
        if (balanceFactor < -1 && getBalanceFactor(retNode.right) <= 0){
            return leftRotate(retNode);
        }

        //LR
        if (balanceFactor > 1 && getBalanceFactor(retNode.left) < 0){
            retNode.left = leftRotate(retNode.left);
            return rightRotate(retNode);
        }

        //Rl
        if (balanceFactor < -1 && getBalanceFactor(retNode.right) > 0){
            retNode.right = rightRotate(retNode.right);
            return leftRotate(retNode);
        }
        return retNode;
    }
    /**
     * 返回以root 为根的二分搜索树的最小值所在的节点
     * @param root 根
     * @return 最小值所在的节点
     */
    private Node minimum(Node root) {
        if (root.left == null){
            return root;
        }
        return minimum(root.left);
    }

    /**
     * 删除最小值所在的节点
     * @return 最小值
     */
    public Node removeMin(){
        Node minimum = minimum(root);
        root = removeMin(root);
        return minimum;
    }

    /**
     * 删除掉以node为根的二分搜索树中最小的节点
     * @param root 根
     * @return 返回新的根
     */
    private Node removeMin(Node root) {
        if (root.left == null){
            Node rightNode = root.right;
            root.right = null;
            size --;
            return rightNode;
        }
        root.left = removeMin(root.left);
        return root;
    }

    public static void main(String[] args) {
        AVLTree<Integer, String> avlTree = new AVLTree();
        for (int i = 0; i < 8; i++) {
            avlTree.add(i,"user" + i);
        }
        avlTree.remove(2);
        System.out.println(avlTree.isBalanced());
        System.out.println(avlTree.isBST());
    }
}
